package com.cennavi.minemap.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.DataBean;
import com.cennavi.minemap.bean.LayerBean;
import com.cennavi.minemap.bean.LayerPerfixTreeBean;
import com.cennavi.minemap.bean.LayerSortBean;
import com.cennavi.minemap.bean.LayerWithPrefixBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.bean.UpdateLayerStyleBean;
import com.cennavi.minemap.service.LayerService;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/layer")
public class LayerController {

    ObjectMapper objectMapper = new ObjectMapper();

    @Autowired(required = false)
    private LayerService layerService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody MessageBean insert(@RequestBody String json) throws Exception {
        LayerBean layerBean = objectMapper.readValue(json, LayerBean.class);
        MessageBean messageBean = new MessageBean();
        List<LayerWithPrefixBean> list = layerService.insert(layerBean);
        if (list.size() < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer insert error");
        } else {
            DataBean<LayerWithPrefixBean> dataBean = new DataBean<LayerWithPrefixBean>();
            messageBean.setErrcode(0);
            dataBean.setTotal(list.size());
            dataBean.setRows(list);
            messageBean.setData(dataBean);
        }
        return messageBean;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody MessageBean update(@RequestBody String json) throws Exception {

        LayerBean layerBean = objectMapper.readValue(json, LayerBean.class);

        MessageBean messageBean = new MessageBean();
        int status = layerService.update(layerBean);
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer update error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }

    @RequestMapping(value = "/update/{field}", method = RequestMethod.POST)
    public @ResponseBody MessageBean updateStyle(@PathVariable String field, @RequestBody String json)
            throws Exception {

        UpdateLayerStyleBean updateLayerStyleBean = objectMapper.readValue(json, UpdateLayerStyleBean.class);

        MessageBean messageBean = new MessageBean();
        int status = layerService.updateStyle(field, updateLayerStyleBean);
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer style update error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }

    @RequestMapping(value = "/delete/{id}")
    public @ResponseBody MessageBean delete(@PathVariable String id) {
        MessageBean messageBean = new MessageBean();
        int status = layerService.delete(id);
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer delete error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }

    @RequestMapping(value = "/{id}")
    public @ResponseBody MessageBean queryById(@PathVariable String id) {
        MessageBean messageBean = new MessageBean();
        List<LayerWithPrefixBean> list = layerService.queryById(id);
        if (list.size() < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer query error");
        } else {
            DataBean<LayerWithPrefixBean> dataBean = new DataBean<LayerWithPrefixBean>();
            messageBean.setErrcode(0);
            dataBean.setTotal(list.size());
            dataBean.setRows(list);
            messageBean.setData(dataBean);
        }
        return messageBean;
    }

    @RequestMapping(value = "/list/{soluId}")
    public @ResponseBody MessageBean queryBySoluId(@PathVariable int soluId) {
        MessageBean messageBean = new MessageBean();
        List<LayerWithPrefixBean> list = layerService.queryBySoluId(soluId);
        if (list.size() < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer list query error");
        } else {
            DataBean<LayerWithPrefixBean> dataBean = new DataBean<LayerWithPrefixBean>();
            messageBean.setErrcode(0);
            dataBean.setTotal(list.size());
            dataBean.setRows(list);
            messageBean.setData(dataBean);
        }
        return messageBean;
    }

    @RequestMapping(value = "/sort/{soluId}")
    public @ResponseBody MessageBean sort(@RequestBody String json, @PathVariable int soluId) throws Exception {

        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, LayerSortBean.class);
        @SuppressWarnings("unchecked")
        List<LayerSortBean> sorts = (List<LayerSortBean>) objectMapper.readValue(json, javaType);

        MessageBean messageBean = new MessageBean();
        int status = layerService.sort(sorts);

        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer sort error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }

    @RequestMapping(value = "/group/update")
    public @ResponseBody MessageBean updateGroup(@RequestBody String json) throws Exception {
        MessageBean messageBean = new MessageBean();
        JSONObject object = JSONObject.parseObject(json);
        String groupId = object.getString("groupId");
        String groupName = object.getString("groupName");
        JSONArray ids = object.getJSONArray("ids");

        int status = layerService.updateGroup(groupId, groupName, ids.toArray(new String[] {}));
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("update layer group error");
        } else {
            messageBean.setErrcode(0);
        }

        return messageBean;
    }

    @RequestMapping(value = "/visibility", method = RequestMethod.POST)
    public @ResponseBody MessageBean updateVisibility(@RequestBody String json) throws Exception {
        MessageBean messageBean = new MessageBean();
        JSONObject object = JSONObject.parseObject(json);
        String visibility = object.getString("visibility");
        JSONArray ids = object.getJSONArray("ids");
        int status = 0;
        for (int i = 0; i < ids.size(); i++) {
            String id = ids.getString(i);
            UpdateLayerStyleBean bean = new UpdateLayerStyleBean();
            bean.setId(id);
            bean.setK("visibility");
            bean.setV(visibility);
            status += layerService.updateStyle("layout", bean);
        }
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("update layer group error");
        } else {
            messageBean.setErrcode(0);
        }

        return messageBean;
    }

    @RequestMapping(value = "/prefix/byCode/{code}")
    public @ResponseBody MessageBean queryLayerPerfixTreeByCode(@PathVariable String code) {
        MessageBean messageBean = new MessageBean();
        List<LayerPerfixTreeBean> list = layerService.queryLayerPerfixTreeByCode(code);
        if (list.size() < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer query error");
        } else {
            DataBean<LayerPerfixTreeBean> dataBean = new DataBean<LayerPerfixTreeBean>();
            messageBean.setErrcode(0);
            dataBean.setTotal(list.size());
            dataBean.setRows(list);
            messageBean.setData(dataBean);
        }
        return messageBean;
    }

    @RequestMapping(value = "/prefix/getTree/{father}")
    public @ResponseBody MessageBean queryLayerPerfixTreeByFather(@PathVariable String father) {
        MessageBean messageBean = new MessageBean();
        List<LayerPerfixTreeBean> list;
        if ("0".equals(father)) {
            list = layerService.queryLayerPerfixTreeByFather(null);
        } else {
            list = layerService.queryLayerPerfixTreeByFather(father);
        }
        if (list.size() < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer query error");
        } else {
            DataBean<LayerPerfixTreeBean> dataBean = new DataBean<LayerPerfixTreeBean>();
            messageBean.setErrcode(0);
            dataBean.setTotal(list.size());
            dataBean.setRows(list);
            messageBean.setData(dataBean);
        }
        return messageBean;
    }

    // @RequestMapping(value = "/batch/update/layout", method =
    // RequestMethod.POST)
    // public @ResponseBody MessageBean updateLayersLayout(@RequestBody String
    // json)
    // throws Exception {
    //
    // JSONObject input = JSONObject.parseObject(json);
    //
    // JSONArray ids = input.getJSONArray("ids");
    // String k = input.getString("k");
    // String v = input.getString("v");
    //
    // List<UpdateLayerStyleBean> list = new ArrayList<UpdateLayerStyleBean>();
    // for(int i = 0 ; i < ids.size() ; i++){
    // UpdateLayerStyleBean updateLayerStyleBean = new UpdateLayerStyleBean();
    // updateLayerStyleBean.setId(ids.getString(i));
    // updateLayerStyleBean.setK(k);
    // updateLayerStyleBean.setV(v);
    // list.add(updateLayerStyleBean);
    // }
    //
    // MessageBean messageBean = new MessageBean();
    //
    // String field = "layout";
    //
    // int status = layerService.updateStyle(field, list);
    // if (status < 0) {
    // messageBean.setErrcode(-1);
    // messageBean.setErrmsg("layer style update error");
    // } else {
    // messageBean.setErrcode(0);
    // }
    // return messageBean;
    // }

    // @RequestMapping(value = "/batch/update/paint", method =
    // RequestMethod.POST)
    // public @ResponseBody MessageBean updateLayersPaint(@RequestBody String
    // json)
    // throws Exception {
    //
    // JSONObject input = JSONObject.parseObject(json);
    //
    // JSONArray ids = input.getJSONArray("ids");
    // String k = input.getString("k");
    // String v = input.getString("v");
    //
    // List<UpdateLayerStyleBean> list = new ArrayList<UpdateLayerStyleBean>();
    // for(int i = 0 ; i < ids.size() ; i++){
    // UpdateLayerStyleBean updateLayerStyleBean = new UpdateLayerStyleBean();
    // updateLayerStyleBean.setId(ids.getString(i));
    // updateLayerStyleBean.setK(k);
    // updateLayerStyleBean.setV(v);
    // list.add(updateLayerStyleBean);
    // }
    //
    // MessageBean messageBean = new MessageBean();
    //
    // String field = "paint";
    //
    // int status = layerService.updateStyle(field, list);
    // if (status < 0) {
    // messageBean.setErrcode(-1);
    // messageBean.setErrmsg("layer style update error");
    // } else {
    // messageBean.setErrcode(0);
    // }
    // return messageBean;
    // }

    @RequestMapping(value = "/batch/update/{field}", method = RequestMethod.POST)
    public @ResponseBody MessageBean updateLayersStyle(@PathVariable String field, @RequestBody String json)
            throws Exception {

        JSONObject input = JSONObject.parseObject(json);

        JSONArray ids = input.getJSONArray("ids");
        String k = input.getString("k");
        String v = input.getString("v");

        List<UpdateLayerStyleBean> list = new ArrayList<UpdateLayerStyleBean>();
        for (int i = 0; i < ids.size(); i++) {
            UpdateLayerStyleBean updateLayerStyleBean = new UpdateLayerStyleBean();
            updateLayerStyleBean.setId(ids.getString(i));
            updateLayerStyleBean.setK(k);
            updateLayerStyleBean.setV(v);
            list.add(updateLayerStyleBean);
        }
        MessageBean messageBean = new MessageBean();

        int status = layerService.updateStyle(field, list);
        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer style update error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }

    @RequestMapping(value = "/prefix/update", method = RequestMethod.POST)
    public @ResponseBody MessageBean updateLayersPrefix(@RequestBody String json) {
        JSONObject input = JSONObject.parseObject(json);

        String[] ids = new String[input.getJSONArray("ids").size()];

        for (int i = 0; i < input.getJSONArray("ids").size(); i++) {
            ids[i] = input.getJSONArray("ids").getString(i);
        }

        String prefix = input.getString("prefix");
        int status = layerService.updateLayersPrefix(prefix, ids);

        MessageBean messageBean = new MessageBean();

        if (status < 0) {
            messageBean.setErrcode(-1);
            messageBean.setErrmsg("layer prefix update error");
        } else {
            messageBean.setErrcode(0);
        }
        return messageBean;
    }
}
